<?php
class models_khachhang extends  connect
{
	function _listKhachHang($start,$limit){
		/*$sql = 'SELECT kh.MaKH, kh.TenKH, kh.DiaChi, kh.SDT,lh.TenLH 
				FROM khachhang kh, loaihang lh
				WHERE kh.MaLH = lh.MaLH';*/
		$sql = "SELECT MaKH, TenKH, DiaChi, SDT, Email
				FROM khachhang LIMIT $start,$limit";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function listkh(){
		
		$sql = "SELECT MaKH, TenKH, DiaChi, SDT, Email
				FROM khachhang ";
		$this->query_db($sql);
		return $this->fetchAll();
	}
	function tongsodong()
		{
			$sql="SELECT * from khachhang";
			$this->query_db($sql);
			return $this->totalRow();
	
		}
	
	function _addkhachhang($TenKH,$DiaChi,$SDT,$Email){
		$sql = "INSERT INTO khachhang VALUES('','$TenKH','$DiaChi','$SDT','$Email')";
		$this->query_db($sql);
	}
	
	function _delKhachHang($id){
		$sql = "DELETE 
				FROM khachhang 
				WHERE MaKH = '$id'";
		
		$this->query_db($sql);
	}
	
	function _getKhachHang($id){
		$sql = "SELECT MaKH, TenKH, DiaChi, SDT, Email
				FROM khachhang 
				WHERE MaKH = '$id'";
		$this->query_db($sql);
		return $this->fetchOne();	
	}
	
	function _updateKhachHang($TenKH, $DiaChi, $SDT,$Email, $id)
	{
		$sql = "UPDATE khachhang 
				SET TenKH = '$TenKH', DiaChi = '$DiaChi', SDT = '$SDT', Email = '$Email'
				WHERE MaKH = $id";
		$this->query_db($sql);
	}
	function getkh($id){
		$sql = "SELECT MaKH, TenKH, DiaChi, SDT, Email
				FROM khachhang 
				WHERE SDT = '$id'";
		$this->query_db($sql);
		return $this->fetchOne();	
	}
} 
	class pager 
{ // Gai tri bat day cua trang
    function findstart($limit) 
    { 
        if ((!isset($_GET['page'])) || ($_GET['page']=="1")) 
        { 
            $start=0; 
            $_GET['page']=1; 
        } 
        else 
        { 
            $start=($_GET['page']-1)*$limit; 
        } 
        return $start; 
    } 
    function findpages($count,$limit) 
    { 
        $pages=(($count % $limit)==0) ? $count / $limit : floor($count / $limit) + 1; 
        return $pages; 
    } 
    function pagelist($curpage , $pages) 
    { 
        $page_list=""; 
        /*in trang dau tien va nhung link toi trang neu co */ 
        if(($curpage !=1) && ($curpage)) 
        { 
            $page_list .="<a href=\""."admin.php?mod=khachhang"."&page=1\" title=\"Trang đầu\"> << </a> "; 
        } 
        if(($curpage-1) > 0) 
        { 
           	$page_list .="<a href=\""."admin.php?mod=khachhang"."&page=".($curpage-1)."\" title=\"Trang trước\"> < </a>"; 	
        } 
        for ($i=1; $i<=$pages; $i++) 
        { 
            if($i==$curpage) 
            { 
                $page_list .="<b>".$i."</b>"; 
            } 
            else 
            { 
                $page_list .="<a href=\""."admin.php?mod=khachhang"."&page=".$i."\" title=\"Trang".$i."\">".$i."</a>"; 
            } 
            $page_list .=" "; 
        } 
        if (($curpage+1) <=$pages) 
        { 
			$page_list .="<a href=\""."admin.php?mod=khachhang"."&page=".($curpage+1)."\" title=\"Trang sau\"> > </a>"; 	
		}
		if(($curpage !=$pages) && ($pages != 0)){
		
            $page_list .="<a href=\""."admin.php?mod=khachhang"."&page=".$pages."\" title=\"Trang cuối\"> >> </a>"; 
        } 
		
        $page_list .="</td>\n\n"; 
        return $page_list; 
    } 
    function nextprev($curpage,$pages) 
    { 
        $next_prev=""; 
        if(($curpage-1)<=0) 
        { 
            $next_prev .="Ve trang truoc";            
        } 
            else 
            { 
                $next_prev .="<a href=\""."admin.php?mod=khachhang"."&page=".($curpage-1)."\"> Trang truoc</a>"; 
            } 
            $next_prev .=" || "; 
            if (($curpage+1) > $pages) 
            { 
                $next_prev .="Den trang sau"; 
            } 
            else 
            { 
                $next_prev .="<a href=\""."admin.php?mod=khachhang"."&page=".($curpage+1)."\"> Trang sau</a>"; 
            } 
            return $next_prev; 
    } 
} 
?>